package com.erytech.linktalk.web.client.dataview;

import com.erytech.linktalk.web.client.GwtSystem;
import com.erytech.linktalk.web.client.datasource.RoomDataSource;
import com.erytech.linktalk.web.client.datasource.UserUpdateEvent;
import com.erytech.linktalk.web.client.framework.DataUpdateEvent;
import com.erytech.linktalk.web.client.framework.DataView;
import com.erytech.linktalk.web.client.i18n.LocaleMessages;
import com.erytech.linktalk.web.client.service.UserInfo;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowCloseListener;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.DialogBox;

public class ClientMainView extends DataView 
	implements WindowResizeListener,	WindowCloseListener {

	LoginView loginView;

	DialogBox loginDialog;

	RoomView roomView;

	DialogBox roomDialog;

	public ClientMainView(RoomDataSource roomDS) {
		super(roomDS);
		loginView = new LoginView(roomDS);
		loginDialog = new DialogBox();
		loginDialog.setWidget(loginView);
		loginDialog.setText(Window.getTitle());
		
		roomView = new RoomView(roomDS);
		roomView.setSize("100%", "100%");
		roomDialog = new DialogBox();
		roomDialog.setText(Window.getTitle());
		roomDialog.setWidget(roomView);
		roomDS.getUserDataSource().addDataUpdateListener(this);
	}

	public void showLogin() {
		loginDialog.setPopupPosition(Window.getClientWidth() / 3, Window
				.getClientHeight() / 8);
		loginDialog.show();
	}

	public void showRoom() {
		roomDialog.show();
	}

	public void initWindow() {
		Window.enableScrolling(false);
		Window.setMargin("0px");
		Window.addWindowResizeListener(this);
		Window.addWindowCloseListener(this);
	}

	public void onWindowResized(int windowWidth, int windowHeight) {
		roomDialog.setWidth((windowWidth - 4) + "px");
		roomDialog.setHeight((windowHeight - 4) + "px");
		int captionHeight = roomView.getAbsoluteTop() 	- roomDialog.getAbsoluteTop();
		roomView.setSize( (windowWidth - 4) + "px", (windowHeight - captionHeight - 4) + "px" );
		roomView.onWindowResized(windowWidth - 4, windowHeight - captionHeight - 4);
	}

	public String onWindowClosing() {
		if (GwtSystem.isDebug()) {
			Window.alert("onWindowClosing");
		}
		return null;
	}

	public void onWindowClosed() {
		if (GwtSystem.isDebug()) {
			Window.alert("onWindowClosed");
		}
		RoomDataSource roomDataSource = (RoomDataSource)getDataSource();
		if (roomDataSource.getUserDataSource().isLogin()) {
			// if(Window.confirm("是否退出聊天室?")) {
			roomDataSource.getUserDataSource().logout();
			// }
		}
	}

	public void onDataUpdateEvent(DataUpdateEvent event) {
		if(event instanceof UserUpdateEvent) {
			UserUpdateEvent userUpdateEvent = (UserUpdateEvent)event;
			if(userUpdateEvent.getType() == UserUpdateEvent.POLL_SUCCESS) {
				UserInfo[] users = (UserInfo[])((UserUpdateEvent)event).getUsers();
				roomDialog.setText(Window.getTitle() + ": " + LocaleMessages.instance.onlineUsers(users.length+1));
			} else if(userUpdateEvent.getType() == UserUpdateEvent.LOGIN_SUCCESS) {
				loginDialog.hide();
				roomDialog.show();
				onWindowResized(Window.getClientWidth(), Window
						.getClientHeight());
			} else if(userUpdateEvent.getType() == UserUpdateEvent.LOGOUT_SUCCESS) {
				roomDialog.hide();
				loginDialog.show();
			}
		}
	}

}